/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.AnnotationClasses;

import ResultEditor.Annotations.Annotation;
import java.awt.Color;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

/**
 *
 * @author Chris 2010-07-18 4:14 pm , Division of Epidemiology, School of Medicine
 */
public class AnnotatedClass {
    public String annotatedClassName;
    public Color  backgroundColor;
    public String source;

    /**
     * Node expanded or not. <p>All classes/markables get displayed as a node
     * in the navigator panel of result editor in eHOST. These nodes may have
     * children which have same classes/markables.
     */
    public boolean isNodeExpanded = false;
    public boolean isVisible = true;

    /**all subAnnotations belongs to this annotated class*/
    protected int annotationsCounter = 0;

    private Vector<AnnotationType> annotationTypes = new Vector<AnnotationType>();


    public AnnotatedClass(){
    }

    public AnnotatedClass(String annotatedClassName, Color backgroundColor, String source, boolean isVisible, boolean isNodeExpanded){
        this.annotatedClassName = annotatedClassName;
        this.backgroundColor = backgroundColor;
        this.source = source;

        this.isNodeExpanded = isNodeExpanded;
        this.isVisible = isVisible;
    }

    public void resetDepot_of_AnnotationTypes(){
        this.annotationTypes.clear();
    }
    public void resetAmount_of_AnnotationCount(){
        this.annotationsCounter = 0;
    }

    public int annotationTypesSize(){
        return annotationTypes.size();
    }

    

    public int getAnnotationAmount(){
        return this.annotationsCounter;
    }

    public int getAnnotationTypeAmount(){
        if ( this.annotationTypes == null )
            return 0;
        return this.annotationTypes.size();
    }

    private void annotationCountPlusPlus(){
        this.annotationsCounter = this.annotationsCounter + 1 ;
    }

    /**to an annotationText, record it if it's a new type; or count++ if existed.*/
    public void recordAnnotation( String annotationText, String filename, Annotation annotation ){
        if ( ( annotationText == null )||( annotationText.trim().length() < 1 ))
            return;

        annotationCountPlusPlus();

        if( this.isAnnotationTypeExisting( annotationText.trim() ) ){
            this.annotationTypePlus( annotationText.trim(), filename, annotation );
        } else {
            AnnotationType at = new AnnotationType( annotationText.trim(), 1 );
            at.addAnnotation(filename, annotation);
            this.annotationTypes.add( at );
        }
    }

    public final Vector<AnnotationType> getAnnotationTypes(){
        sort();
        return this.annotationTypes;
    }

    public void sort(){
        Comparator comp = new AnnotationTypeComparator();
        Collections.sort( this.annotationTypes , comp);
    }

    private void annotationTypePlus(String annotationText, String filename, Annotation annotation ){
        if ( ( annotationText == null )||( annotationText.trim().length() < 1 ))
            return;

        for( AnnotationType annotationtype : annotationTypes ){
            if ( annotationtype == null )
                continue;
            if( annotationtype.name.compareTo( annotationText.trim() ) == 0 ){
                annotationtype.amount = annotationtype.amount + 1;
                annotationtype.addAnnotation(filename, annotation);
                return;
            }
        }
    }

    /**check existing of a type of annotationText. */
    private boolean isAnnotationTypeExisting( String annotation ){
        if ( ( annotation == null )||( annotation.trim().length() < 1 ))
            return false;

        for( AnnotationType annotationtype : annotationTypes ){
            if ( annotationtype == null )
                continue;
            if( annotationtype.name.compareTo( annotation.trim() ) == 0 ){
                return true;
            }
        }
        return false;
    }

    /**
     * Set flag to indicate node expanded or not. <p>
     * All classes/markables get displayed as a node in the navigator panel of
     * result editor in eHOST. These nodes may have children which have same
     * classes/markables.
     *
     * @param   flag
     *          The status of node of this class/markable you want to set. True
     *          means expanded; false means not expanded.
     */
    public void setExpansionStatus(boolean flag){
        this.isNodeExpanded = flag;
    }
}
